package com.kaibes.imserver.model.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kaibes.imserver.KaibesIMServer;

public class ContactsInformationsData {

	private ArrayList<HashMap<String, Object>> informations = null;

	public ContactsInformationsData() {
		informations = new ArrayList<>();
	}

	public void addContactInformationsData(ResultSet resultSet) {
		try {
			if (resultSet != null) {
				while (resultSet.next()) {

					HashMap<String, Object> information = new HashMap<String, Object>();
					String contactname = resultSet.getString("contactname");

					information.put("contactname", contactname);
					information
							.put("nickname", resultSet.getString("nickname"));
					information.put("photo", resultSet.getString("photo"));
					information.put("sex", resultSet.getString("sex"));
					information.put("birth", resultSet.getString("birth"));
					information.put("signature",
							resultSet.getString("signature"));
					information.put("introduction",
							resultSet.getString("introduction"));
					information.put("job", resultSet.getString("job"));
					information.put("company", resultSet.getString("company"));
					information.put("school", resultSet.getString("school"));
					information.put("address", resultSet.getString("address"));
					information
							.put("hometown", resultSet.getString("hometown"));
					information.put("email", resultSet.getString("email"));
					information.put("remark", resultSet.getString("remark"));

					boolean isOnline = KaibesIMServer.mainClientControl
							.getMainSocketControl(contactname) == null ? false
							: true;
					information.put("online", isOnline);
					informations.add(information);
				}
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		;
	}

	public JSONArray toJSONArray() {
		JSONArray jsonArray = null;

		if (informations.size() > 0) {
			jsonArray = new JSONArray();
			for (int i = 0; i < informations.size(); i++) {
				HashMap<String, Object> information = informations.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("contactname", information.get("contactname"));
				jsonObject.put("nickname", information.get("nickname"));
				jsonObject.put("photo", information.get("photo"));
				jsonObject.put("sex", information.get("sex"));
				jsonObject.put("birth", information.get("birth"));
				jsonObject.put("signature", information.get("signature"));
				jsonObject.put("introduction", information.get("introduction"));
				jsonObject.put("job", information.get("job"));
				jsonObject.put("company", information.get("company"));
				jsonObject.put("school", information.get("school"));
				jsonObject.put("address", information.get("address"));
				jsonObject.put("hometown", information.get("hometown"));
				jsonObject.put("email", information.get("email"));
				jsonObject.put("remark", information.get("remark"));

				jsonObject.put("online", information.get("online"));
				jsonArray.put(jsonObject);
			}
		}
		return jsonArray;
	}

	public int size() {
		return informations.size();
	}
}
